<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
use GuzzleHttp\Client;

if (!function_exists("SendError")){
    /**
     * @param $msg
     * @param array $data
     * @param int $code
     * @return \think\response\Json
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @describe:返回错误信息数组
     */
    function SendError($msg, $data=[], int $code = 0)
    {
        $result = [
            'code' => $code,
            'msg'  => $msg?$msg:'error',
            'time' => time(),
            'data' => $data,
        ];
        return  json($result);
    }
}
if (!function_exists("OrderSn")){
    function OrderSn(){
        $OrderSn = 'T'.date('Ymd',time()).rand(10000,99999);
        $model =  new \app\common\model\tang\Cost();
        $is_sn = $model->where('order_sn',$OrderSn)->find();
        if($is_sn){
            OrderSn();
        }
        return $OrderSn ;
    }
}
if (!function_exists("MarrerSn")) {
    function MarrerSn()
    {
        $OrderSn = 'M' . date('Ymd', time()) . rand(10000, 99999);
        $model = new \app\common\model\tang\RepairMarrer();
        $is_sn = $model->where('order_sn', $OrderSn)->find();
        if ($is_sn) {
            MarrerSn();
        }
        return $OrderSn;
    }
}

if (!function_exists("SendSuccess")){
    /**
     * @param $msg
     * @param array $data
     * @param int $code
     * @return \think\response\Json
     * @author: LuckyHhy <jackhhy520@qq.com>
     * @describe:返回成功信息数组
     */
    function SendSuccess($msg, $data=[], int $code = 1)
    {
        $result = [
            'code' => $code,
            'msg'  => $msg?$msg:'error',
            'time' => time(),
            'data' => $data,
        ];
        return  json($result);
    }
    function RequestServer($url,$param_data=[],$mch_id='',$debug=true,$appid='hefeizhengda',$appSecret='mimashizhengda')
    {
        $request_data =[];
        $client = new Client();
        $param_data['mch_id'] = $mch_id;
        $param_data['sp_name'] = 'HFZDISP';
        $request_data['json'] = $param_data;
        $request_url = "https://inner.api.kelimx.com/cgi-bin/$url";
        if ($debug){
            if (empty($appid) || empty($appSecret)) {
                throw new \Exception('调试模式下,请填写appId和appSecret',403);
            }
            $sendToken = file_get_contents("https://api.kelimx.com/cgi-bin/token?appId=$appid&appSecret=$appSecret");
            $json = json_decode($sendToken,true);
            $request_data['headers']['x-access-token'] = $json['result']['access_token']; //调试模式下 加token
            $request_url = "https://api.kelimx.com/cgi-bin/$url"; //调试模式下修改请求地址
        }
        try {
            $response = $client->post($request_url,$request_data);
            $reqs = json_decode($response->getBody()->getContents(),true);
            if ($response->getStatusCode() == 200){
                return $reqs['result'];
            }
        }catch (\GuzzleHttp\Exception\ClientException $exception) {
            $contents  = json_decode($exception->getResponse()->getBody()->getContents(),true);
            $result = $contents['result']??$exception->getMessage();  // 兼容msg
            $message = isset($contents['msg']) && $contents['msg'] != 'Bad Request' ? $contents['msg']:$result; // 兼容msg
            throw new \Exception($message,$contents['code']??$exception->getCode());
        }
    }
}